package Bill.app.serveur;

import java.io.*;
import java.net.Socket;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;

import Bill.app.serveur.bean.Amis;
import Bill.app.serveur.bean.dao.implement.MainDaoImplement;

 
class ThreadRaptrieAmi extends Thread
{

    ThreadRaptrieAmi(Socket paquet, String mess, Hashtable LesConnecter)
    {
        this.paquet = null;
        this.LesConnecter = null;
        this.paquet = paquet;
        this.mess = mess;
        this.LesConnecter = LesConnecter;
        start();
    }

    public void run()
    {
        PrintWriter ecriture = null;
        try
        {
            ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(paquet.getOutputStream())), true);
        }
        catch(IOException e3)
        {
            e3.printStackTrace();
        }
        String text = null;
        text = mess;
        System.out.println((new StringBuilder("message re\347u:")).append(text).toString());
        StringTokenizer t = new StringTokenizer(text, ":");
        t.nextToken();
        String login = t.nextToken();
        String donnee = null;
        try
        {
            donnee = chargerAmi(login.trim());
        }
        catch(Throwable e1)
        {
            e1.printStackTrace();
        }
        String mes = "";
        try
        {
            ecriture.println(donnee);
            ecriture.close();
        }
        catch(Throwable e)
        {
            ecriture.println("null");
        }
        Thread.yield();
    }

    public String chargerAmi(String login)
        throws Throwable
    {
        String donne = "";
          MainDaoImplement em = SERVEUR.getEm();
        List<Amis> result = em.loadAmis(login);
         String Status = "";
         int j=0;
        while(j<result.size()) 
        {
            String log = result.get(j).getAmis().getLogin() ;
            if(LesConnecter.containsKey(log))
                Status = "en_ligne";
            else
                Status = "hors_ligne";
            donne = (new StringBuilder(String.valueOf(donne))).append(log).append("     ").append(Status).append("=").toString();
       j++;
        }
        System.out.println(donne);
         
        return donne;
    }

    Socket paquet;
    String mess;
    Hashtable LesConnecter;
}
